<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//商品控制器
class GoodsController extends Controller{
	private $model;
	public function __init(){
	    //调用CommonController里面的构造方法__init()(包含验证是否登录)
	    parent::__init();
		$this->model=new \Common\Model\Goods;
	}
	//商品展示
	public function index(){
		//接受数据
		$data=$this->model->get();
//		p($data);
		View::with('data',$data);
	    View::make();
	}
	//商品添加
	public function add(){
		if(IS_POST){
//			p($_POST);die;
			
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功',U('index'));
		}
		//1处理所属分类
		$model=new \Common\Model\Cate;
		$cateData = Data::tree($model->get(),'cname');
//		p($cateData);
		View::with('cateData',$cateData);
		//2处理所属品牌
		$model=new \Common\Model\Band;
		$bandData=$model->get();
		View::with('bandData',$bandData);
	    View::make();
	}
	
	public function ajaxGetAttr(){
		if(IS_AJAX){
			//通过类型id获得类型属性
			$tid = Q('post.id',0,'intval');
			$model=new \Common\Model\Typeattr;
			$data =$model->where("type_tid={$tid}")->get();
//			p($data);
			//如果有类型属性
			foreach ($data as $k => $v) {
				//把tavalue字符串变为数组
				$data[$k]['tavalue'] = explode('|', $v['tavalue']);
			}
			echo json_encode($data);
			exit;
		}
	}
	//商品编辑
	public function edit(){
		$gid=Q('get.gid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('编辑成功',U('index'));
		}
		//1获得商品数据
		$goodsData=$this->model->where("gid='{$gid}'")->find('cate_cid');
		View::with('goodsData',$goodsData);
		//2处理所属分类
		$GoodeCate=new \Common\Model\Cate;
		$cateData = Data::tree($GoodeCate->get(),'cname');
//		p($cateData);
		View::with('cateData',$cateData);
		//3处理所属品牌
		$GoodsBand=new \Common\Model\Band;
		$bandData=$GoodsBand->get();
		View::with('bandData',$bandData);
		//4获得商品旧数据
		$oldData=$this->model->where("gid='{$gid}'")->find();
//		p($oldData);
		View::with('oldData',$oldData);
//		p($oldData);
		//5获得商品详细数据
		$goodsDetail=new \Common\Model\Detail;
		$DetailData=$goodsDetail->where("goods_gid='{$gid}'")->find();
		$DetailData['dpic']=explode('|', $DetailData['dpic']);
		View::with('DetailData',$DetailData);
//		p($DetailData);
		//6获得商品属性表的数据
		$goodsattr=new \Common\Model\Goodsattr;
		$goodsattrData=$goodsattr->join('typeattr','typeattr_taid','=','taid')->where("goods_gid='{$gid}'")->get();
		array_walk($goodsattrData,function(&$v,$k){
			if(!empty($v['tavalue'])){
				//把字符串转化为数组
				$v['tavalue']=explode('|', $v['tavalue']);
			}
		});
		View::with('goodsattrData',$goodsattrData);
//		p($goodsattrData);
	    View::make();
	}
	
	//图片上传方法
	//php用法
	//uploadify异步上传
	public function upload()
	{
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
	                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
	                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
	                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	public function del(){
		//1删除商品表
	    $gid=Q('get.gid',0,'intval');
		$path=$this->model->where("gid={$gid}")->pluck('pic');
		if(file_exists($path))
			//删除图片
			unlink($path);
		//删除数据
		$this->model->where("gid={$gid}")->delete();
		//2删除 商品属性表
		$goodsAttr=new \Common\Model\Goodsattr;
		$goodsAttr->where("goods_gid='{$gid}'")->delete();
		//3删除商品详细表
		$detail=new \Common\Model\Detail;
		//获得图片路径
		$data=$detail->where("goods_gid='{$gid}'")->pluck('dpic');
//		p($data);exit;
		//把字符变为数组
		$data=explode('|', $data);
		foreach ($data as $v) {
			if(file_exists($v))unlink($v);
		}
		$detail->where("goods_gid='{$gid}'")->delete();
		View::success('删除成功',U('index'));
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

 ?>